package com.zhong.gang.app.ui.zhongzhuan.fa;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import com.luck.picture.lib.tools.ToastUtils;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.databinding.ActivityZhongZhuanFaBinding;
import com.zhong.gang.app.ui.address.AddressBean;
import com.zhong.gang.app.mvp.CommonPresenter;
import com.zhong.gang.app.ui.main.MainActivity;
import com.zhong.gang.app.utils.AddressListener;
import com.zhong.gang.applib.CheseListener;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.applib.rx.RxManager;
import com.zhong.gang.applib.ui.chese.MaterialTypeBean;
import java.io.File;

public class ZhongZhuanFaPersenter implements CheseListener , AddressListener {
    private ActivityZhongZhuanFaBinding binding;
    private MaterialTypeBean materialTypeBean;
    private AddressBean addressBean;
    private Activity activity;

    public AddressBean getAddressBean() {
        return addressBean;
    }

    public ZhongZhuanFaPersenter(Activity activity, ActivityZhongZhuanFaBinding binding) {
        this.activity = activity;
        this.binding = binding;
        binding.material.setText(binding.add,binding.remove);
    }


    @Override
    public void onCheseBack(MaterialTypeBean bean) {
        this.materialTypeBean = bean;
        binding.typeTv.setText(bean.getName());
    }
    /**
     * 下一步
     */
    public void next(File halfile,File allFile){
        String list = binding.material.getJsonArray();
        String bch = binding.bch.getText().toString();
        if (materialTypeBean == null){
            ToastUtils.s(activity,"请选择钢厂");
            return;
        }
        if (TextUtils.isEmpty(bch)){
            ToastUtils.s(activity,"请填写班次号");
            return;
        }
        if (addressBean == null){
            ToastUtils.s(activity,"请选择到货料型");
            return;
        }
        if (TextUtils.isEmpty(list)){
            ToastUtils.s(activity,"请填写料型类型");
            return;
        }
        if (halfile == null){
            ToastUtils.s(activity,"请选择半仓照片");
            return;
        }
        if (allFile == null){
            ToastUtils.s(activity,"请选择满仓照片");
            return;
        }
        upHalf(activity,halfile,allFile,addressBean.getId(),bch,materialTypeBean.getId(),list);
    }

    /**
     * 上传图片
     */
    public void upHalf(Activity activity,File halfFile,File allFile,int steelMillId, String bch, int steelMillMaterialTypeId, String list){
        CommonPresenter.upImg(activity,halfFile,new DataCallBack<BaseDataBean<String>>(activity){
            @Override
            public void onResponse(BaseDataBean<String> data) throws Exception {
                upAll(activity,steelMillId,bch,steelMillMaterialTypeId,data.getResult(),allFile,list);
            }
        });
    }


    /**
     * 上传图片
     */
    public void upAll(Activity activity, int steelMillId, String bch, int steelMillMaterialTypeId, String halfStr,File allFile, String list){
        CommonPresenter.upImg(activity,allFile,new DataCallBack<BaseDataBean<String>>(activity){
            @Override
            public void onResponse(BaseDataBean<String> data) throws Exception {
                upMsg(activity,steelMillId,bch,steelMillMaterialTypeId,halfStr,data.getResult(),list);
            }
        });
    }


    public void upMsg(Activity activity, int steelMillId, String bch, int steelMillMaterialTypeId, String falfPhoto, String fullPhoto, String list){
        HttpUtils.post(Api.transferSend,activity)
                .addParams("steelMillId",steelMillId+"")
                .addParams("bch",bch)
                .addParams("steelMillMaterialTypeId",steelMillMaterialTypeId+"")
                .addParams("falfPhoto",falfPhoto)
                .addParams("fullPhoto",fullPhoto)
                .addParams("materialTypeList",list)
                .build().execute(activity,"提交信息",new DataCallBack<BaseDataBean<String>>(activity){
            @Override
            public void onResponse(BaseDataBean data) throws Exception {
                super.onResponse(data);
                new RxManager().post(MainActivity.MAIN_ADD_OK);
                activity.finish();
            }
        });
    }

    @Override
    public void onAddressBack(AddressBean addressBean) {
        Log.e("==``",addressBean.getId()+"---");
        binding.gangchangTv.setText(addressBean.getName());
        if (addressBean == null || addressBean.getId() != addressBean.getId()) {
            binding.typeTv.setText("");
            this.materialTypeBean = null;
        }
        this.addressBean = addressBean;
        binding.typeTv.setText("");
        this.materialTypeBean = null;
        binding.material.setType(addressBean.getId(),1);
    }
}

